
import UtilFuncs from '../../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import globalConst from '../../common/globalConst.js';
var global = globalConst.GlobalConst;

import LXCOM from '../../common/common.js'

var AbsorCylinderPos = {}

AbsorCylinderPos.calculate = function (pDstGraph, mousePt, offsetPt) {
    var x = mousePt.x;
    var y = mousePt.y;
    var absobPt = { x, y };
    let mindis = this.absorb2LeftRightEdge(pDstGraph, mousePt, offsetPt, absobPt);
    mindis = this.absorb2TopEclipse(pDstGraph, mousePt, absobPt, mindis);
    mindis = this.absorb2BottomEclipse(pDstGraph, mousePt, absobPt, mindis);
    return absobPt;
}

AbsorCylinderPos.absorb2BottomEclipse = function (pDstGraph, mousePt, absobPt, mindis) {
    let halfwid = pDstGraph.width / 2;
    let halfhgt = pDstGraph.height / 2;
    let arcRadius = pDstGraph.arcRadius;

    let cntPt = pDstGraph.getPosCopy();
    cntPt.y = cntPt.y - halfhgt + arcRadius;

    let vector = funcs.getVector(cntPt, mousePt);
    let degree = vector.angleFrmXAxisPositiveDir();
    if (degree<180 && degree > 0){
        return mindis;
    }
    let radVal = funcs.degree2Radian(degree);

    let a = halfwid;
    let b = arcRadius;
    let x = 0;
    let y = 0;
    if (Math.abs(vector.m) < global.ZERO_8E) {
        x = 0;
        if (vector.n > 0)
            y = arcRadius;
        else
            y = -arcRadius;
    }
    else {
        let k = vector.n / vector.m;
        let t = 0;
        if (vector.m > 0) {
            t = Math.sqrt(a * a * k * k + b * b);
        }
        else if (vector.m < 0) {
            t = -Math.sqrt(a * a * k * k + b * b);
        }
        x = a * b / t;
        y = k * x;
    }


    x += cntPt.x;
    y += cntPt.y;
   
    let dis = funcs.distancePoint({ x, y }, mousePt, 1);
    if (dis < mindis || mindis < 0) {
        absobPt.x = x;
        absobPt.y = y;
        mindis = dis;
    }
    return mindis;
}

AbsorCylinderPos.absorb2TopEclipse = function (pDstGraph, mousePt, absobPt, mindis) {
    let halfwid = pDstGraph.width / 2;
    let halfhgt = pDstGraph.height / 2;
    let arcRadius = pDstGraph.arcRadius;

    let cntPt = pDstGraph.getPosCopy();
    cntPt.y = cntPt.y + halfhgt - arcRadius;

    let vector = funcs.getVector(cntPt, mousePt);

    let a = halfwid;
    let b = arcRadius;
    let x = 0;
    let y = 0;
    if (Math.abs(vector.m) < global.ZERO_8E) {
        x = 0;
        if (vector.n > 0)
            y = arcRadius;
        else
            y = -arcRadius;
    }
    else {
        let k = vector.n / vector.m;
        let t = 0;
        if (vector.m > 0) {
            t = Math.sqrt(a * a * k * k + b * b);
        }
        else if (vector.m < 0) {
            t = -Math.sqrt(a * a * k * k + b * b);
        }
        x = a * b / t;
        y = k * x;
    }


    x += cntPt.x;
    y += cntPt.y;
    let degree = vector.angleFrmXAxisPositiveDir();
    let radVal = funcs.degree2Radian(degree);
    // let x = cntPt.x + halfwid*Math.cos(radVal);
    // let y = cntPt.y + arcRadius*Math.sin(radVal);
    let dis = funcs.distancePoint({ x, y }, mousePt, 1);
    if (dis < mindis || mindis < 0) {
        absobPt.x = x;
        absobPt.y = y;
        mindis = dis;
    }
    return mindis;
}

AbsorCylinderPos.absorb2LeftRightEdge = function (pDstGraph, mousePt, offsetPt, absobPt) {
    let halfwid = pDstGraph.width / 2;
    let halfhgt = pDstGraph.height / 2;
    let depth = pDstGraph.arcRadius;

    let mindis = -1;
    let mtx = pDstGraph.getLocalMatrix();
    let x = 0;
    let y = 0;

    //左侧竖直边线
    x = -halfwid;
    y = halfhgt - depth;
    let prePt = mtx.MultT({ x, y, z: 0 });
    prePt.x = prePt.x + offsetPt.x;
    prePt.y = prePt.y + offsetPt.y;

    x = -halfwid;
    y = -halfhgt + depth;
    let nextPt = mtx.MultT({ x, y, z: 0 });
    nextPt.x = nextPt.x + offsetPt.x;
    nextPt.y = nextPt.y + offsetPt.y;

    var tmpPoint = { x: absobPt.x, y: absobPt.y };
    var dis = this.pt2LineSecDis(prePt.x, prePt.y, nextPt.x, nextPt.y, tmpPoint, mousePt);
    if (dis >= 0) {
        if (dis < mindis || mindis < 0) {
            mindis = dis;
            absobPt.x = tmpPoint.x;
            absobPt.y = tmpPoint.y;
        }
    }

    //右侧竖直边线
    x = halfwid;
    y = halfhgt - depth;
    prePt = mtx.MultT({ x, y, z: 0 });
    prePt.x = prePt.x + offsetPt.x;
    prePt.y = prePt.y + offsetPt.y;

    x = halfwid;
    y = -halfhgt + depth;
    nextPt = mtx.MultT({ x, y, z: 0 });
    nextPt.x = nextPt.x + offsetPt.x;
    nextPt.y = nextPt.y + offsetPt.y;

    tmpPoint = { x, y };
    var dis = this.pt2LineSecDis(prePt.x, prePt.y, nextPt.x, nextPt.y, tmpPoint, mousePt);
    if (dis >= 0) {
        if (dis < mindis || mindis < 0) {
            mindis = dis;
            absobPt.x = tmpPoint.x;
            absobPt.y = tmpPoint.y;
        }
    }

    return mindis;
}

AbsorCylinderPos.pt2LineSecDis = function (x1, y1, x2, y2, absobPt, mousePt) {
    let x = mousePt.x;
    let y = mousePt.y;
    var tmpPoint = { x, y };
    x = x1;
    y = y1;
    var pt1 = { x, y };

    x = x2;
    y = y2;
    var pt2 = { x, y };

    var linesec = new LXCOM.LGXLineSection();
    linesec.setPt(pt1, pt2);
    var dis = funcs.pt2LineSec(mousePt, linesec, tmpPoint);
    absobPt.x = tmpPoint.x;
    absobPt.y = tmpPoint.y;
    return dis;
}

export default { AbsorCylinderPos }